package com.t235.gc.device.service;


import com.t235.gc.dao.DeviceCabinetDao;
import com.t235.gc.dao.DeviceDao;
import com.t235.gc.dao.DeviceFaultRecordDao;
import com.t235.gc.entity.Device;
import com.t235.gc.entity.DeviceCabinet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeviceService {

    @Resource
    private DeviceDao deviceDao;
    @Resource
    private DeviceCabinetDao deviceCabinetDao;
    @Resource
    private DeviceFaultRecordDao deviceFaultRecordDao;

    public Integer selectDeviceStatusCount(Integer status,Integer isStock){
        return deviceDao.selectDeviceStatusCount(status,isStock);
    }

    public List<Device> selectDevicePageBean(Device device){
        return deviceDao.selectDevicePageBean(device);
    }

    public Integer saveOrUpdateDevice(Device device, DeviceCabinet deviceCabinet) {
        Integer i = 0;
        if(device.getDeviceid() == null || device.getDeviceid() == 0){
            Integer rows = deviceCabinet.getRownumber();
            Integer columns = deviceCabinet.getColumnnumber();
            device.setDeviceid(System.currentTimeMillis());
            DeviceCabinet deviceCabinet1 = deviceCabinet;
            deviceCabinet1.setDeviceid(device.getDeviceid());
            if(deviceDao.insertSelective(device) > 0){
                for (int row = 1;row <= rows;row++){
                    for(int column = 1;column <= columns;column++){
                        deviceCabinet1.setRownumber(row);
                        deviceCabinet1.setColumnnumber(column);
                        i = deviceCabinetDao.insertSelective(deviceCabinet);
                    }
                }
            }
        }else{
            i = deviceDao.updateByPrimaryKeySelective(device);
        }
        return i;
    }

    public Integer delDevice(Long id) {
        deviceFaultRecordDao.updateDeviceFaultRecordIsDel(id);
        return deviceDao.updateDeviceIsDel(id);
    }

    public Integer deviceVersionUpgrade(Long id, Integer versionId) {
        return deviceDao.deviceVersionUpgrade(id,versionId);
    }

    public Integer addDevice(Device device, DeviceCabinet deviceCabinet) {
        if(deviceDao.insertSelective(device)>0){
            return deviceCabinetDao.insertSelective(deviceCabinet);
        }
        return 0;
    }
}
